<template>
	<view class="container">
		<view class="content">
			<view class="merchant">
				<view class="merchant_box" v-for="(item, index) in list" :key="index" @tap="onSelect(item)">
					<image :src="item.business_img"></image>
					<view class="merchant_info">
						<view class="merchant_title">{{item.business_name}}</view>
						<view class="merchant_content">
							<view class="merchant_address">
								<view class="address_text">{{item.area}}</view>
								<view class="address_distance">{{item.distance}}m</view>
							</view>
							<view class="merchant_orientation" @click.stop="openLocation(item)">
								<image src="/static/images/merchant_orientation@2x.png"></image>
								<view class="orientation_text">到这去</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<loading-footer :status="loadingStatus" slotEmpty>
				<view class="data-null column-center" slot="empty">
					<image class="img-null" src="/static/images/order_null.png" />
					<text class="nr muted">暂无记录～</text>
				</view>
			</loading-footer>
		</view>
	</view>
</template>

<script>
	import {
		toLogin
	} from '@/utils/login'
	import {
		businessList
	} from '@/api/common.js'
	import wechath5 from '@/utils/wechath5'
	import {
		loadingType
	} from "@/utils/type";
	import {
		loadingFun
	} from "@/utils/tools"
	export default {
		data() {
			return {
				list: [],
				page: 1,
				loadingStatus: loadingType.LOADING
			};
		},
		onLoad() {
			this.businessFun()
		},
		onReachBottom() {
			this.businessFun()
		},
		methods: {
			onSelect(e) {
				uni.$emit('selectaddress', e);
				uni.navigateBack();
			},
			//根据经纬度计算距离
			distance(lat1, lng1) {
				let location = uni.getStorageSync('location')
				let lat2 = location.latitude;
				let lng2 = location.longitude;
				let rad1 = parseFloat(lat1) * Math.PI / 180.0;
				let rad2 = parseFloat(lat2) * Math.PI / 180.0;
				let a = rad1 - rad2;
				let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;

				let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(
					rad2) * Math.pow(Math.sin(b / 2), 2)));
				s = s * 6378.137;
				s = Math.round(s * 10000) / 10000;
				s = s.toString();
				s = s.substring(0, s.indexOf('.') + 2);
				return s; //返回距离
			},
			openLocation(item) {
				let data = {
					latitude: parseFloat(item.lat),
					longitude: parseFloat(item.lng),
					name: item.business_name,
					address: `${item.province} ${item.city} ${item.district} ${item.address}`,
					key: 'QCQBZ-L2WCN-OAGFC-SERS5-DK357-HQBYU' // 腾讯地图 key
				}
				wechath5.openLocation(data)
			},
			businessFun() {
				let location = uni.getStorageSync('location')
				let {
					list,
					loadingStatus,
					page
				} = this;
				loadingFun(businessList, page, list, loadingStatus,{
					lat: location.latitude,
					lng: location.longitude,
					}).then(res => {
					if (res) {
						this.page = res.page;
						this.list = res.dataList;
						this.loadingStatus = res.status
					}
				})
			},
		}

	};
</script>
<style lang="scss">
	.container {
		.content {
			padding: 0 25rpx;
			padding-bottom: 120rpx;

			.merchant {
				.merchant_box {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					margin-bottom: 20rpx;
					padding: 30rpx;
					background: #FFFFFF;
					border-radius: 10rpx;

					image {
						width: 230rpx;
						height: 160rpx;
					}

					.merchant_info {
						margin-left: 20rpx;

						.merchant_title {
							width: 400rpx;
							font-size: 28rpx;
							font-family: PingFang SC;
							font-weight: bold;
							color: #1D1F24;
							line-height: 40rpx;
						}

						.merchant_content {
							display: flex;
							justify-content: space-between;
							width: 400rpx;
							margin-top: 30rpx;

							.merchant_address {
								display: flex;
								flex-direction: column;
								justify-content: space-between;

								.address_text {
									font-size: 22rpx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #4D4D4D;
									line-height: 48rpx;
								}

								.address_distance {
									font-size: 22rpx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #808080;
									line-height: 48rpx;
								}
							}

							.merchant_orientation {
								display: flex;
								flex-direction: column;
								justify-content: space-between;
								align-items: center;

								image {
									width: 50rpx;
									height: 50rpx;
								}

								.orientation_text {
									margin-top: 10rpx;
									font-size: 24rpx;
									font-family: PingFang SC;
									font-weight: 500;
									color: #808080;
									line-height: 48rpx;
								}
							}
						}
					}
				}
			}
		}
	}
</style>